﻿#region

using System.Collections.Generic;
using System.Linq;
using Lab.DataContext.DataContext;
using Lab.Server.Domain;

#endregion

namespace Lab.Server.Infrastructure {
    public class ApplicantRepository : Repository<Applicant, applicant> {
        public ApplicantRepository(IDataContext dataContext) : base(dataContext) {}

        protected override applicant TranslateToDataObject(Applicant domainObject) {
            var dataObject = new applicant {
                                               surname_txt = domainObject.Surname,
                                               name_txt = domainObject.Name,
                                               middle_name_txt = domainObject.MiddleName,
                                               place_of_residence = domainObject.PlaceOfResidence,
                                               kind_of_activity = new KindOfActivityRepository(DataContext)
                                                   .GetKindOfActivityDataObject(domainObject.KindOfActivity),
                                           };

            foreach (var skill in domainObject.Skills) {
                dataObject.skills_of_applicants
                          .Add(new SkillRepository(DataContext).GetSkillOfApplicant(skill));
            }

            return dataObject;
        }

        public applicant GetApplicantDataObject(Applicant applicant) {
            return DataContext.GetApplicants().Single(_ => _.applicant_id == applicant.Id);
        }

        public List<Applicant> GetApplicants() {
            return DataContext.GetApplicants().ConvertAll(ToDomainObject);
        }

        public Applicant ToDomainObject(applicant applicant) {
            var skillRepository = new SkillRepository(DataContext);
            return new Applicant {
                                     Id = applicant.applicant_id,
                                     Surname = applicant.surname_txt,
                                     MiddleName = applicant.middle_name_txt,
                                     Name = applicant.name_txt,
                                     PlaceOfResidence = applicant.place_of_residence,
                                     KindOfActivity = new KindOfActivityRepository(DataContext)
                                         .TranslateToDomain(applicant.kind_of_activity),
                                     Skills = applicant.skills_of_applicants
                                                       .ToList()
                                                       .ConvertAll(_ => skillRepository.TranslateToDomain(_.skill))
                                 };
        }
    }
}